package com.servlet;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class Demo01Servlet implements Servlet {
    public Demo01Servlet() {
        System.out.println("Demo01Servlet 构造函数被调用");
    }

    @Override
    public void init(ServletConfig config) throws ServletException {
        System.out.println("Demo01Servlet 初始化");
    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) req;
        String method = request.getMethod();
        if (method.equals("GET")) {
            doGet(request, (HttpServletResponse) res);
        } else if (method.equals("POST")) {
            doPost(request, (HttpServletResponse) res);
        }
        System.out.println("Demo01Servlet 被访问");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("进入doGet");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("进入 doPost");
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {
        System.out.println("Demo01Servlet 被摧毁");
    }
}
